package com.zjp.birthdaymanager.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zjp.birthdaymanager.AddBirthActivity;
import com.zjp.birthdaymanager.BirthDetailActivity;
import com.zjp.birthdaymanager.R;
import com.zjp.birthdaymanager.adapter.BirthAdapter;
import com.zjp.birthdaymanager.been.BirthInfo;
import com.zjp.birthdaymanager.database.DataBaseHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/17.
 */
public class BirthdayFragment extends Fragment implements View.OnClickListener {

    private TextView tv_title;
    //添加生日
    private ImageView iv_right;
    //生日listView
    private ListView lvBirth;
    //某个人的生日信息
    private List<BirthInfo> mListBirthInfo = new ArrayList<BirthInfo>();
    private BirthAdapter birthAdapter;

    private SQLiteDatabase db;
    private DataBaseHelper dbHelper;
    private Cursor cursor;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        dbHelper = new DataBaseHelper(getActivity(), "birth.db");
        db = dbHelper.getWritableDatabase();

        View view = initView();
        return view;
    }

    public View initView() {
        View view = View.inflate(getActivity(), R.layout.fragment_birth, null);
        iv_right = (ImageView) view.findViewById(R.id.iv_titleBar_right);
        tv_title = (TextView) view.findViewById(R.id.tv_titleBar_title);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("生日");
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(R.mipmap.btn_add);
        iv_right.setOnClickListener(this);

        lvBirth = (ListView) view.findViewById(R.id.birth_listview);
        birthAdapter = new BirthAdapter(getActivity());
        lvBirth.setAdapter(birthAdapter);

        lvBirth.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(getActivity(),BirthDetailActivity.class);
                intent.putExtra("strName", mListBirthInfo.get(position).strName);
                intent.putExtra("strBirthdate",mListBirthInfo.get(position).strBirthdate);
                intent.putExtra("strPhone",mListBirthInfo.get(position).strPhone);
                intent.putExtra("strRemark",mListBirthInfo.get(position).strRemark);
                startActivity(intent);
            }
        });



        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_titleBar_right:
                Intent intent = new Intent(getActivity(), AddBirthActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        mListBirthInfo.clear();
        select();
        Log.i("info", "BirthFragment onStart");
    }

    public void select() {

        cursor = db.query(getActivity().getString(R.string.tablename), null, null, null, null, null, null);
        if (cursor != null) {

            Log.i("info", cursor.toString());

            while (cursor.moveToNext()) {
                String strName = cursor.getString(cursor.getColumnIndex("name"));
                String strBirthDate = cursor.getString(cursor.getColumnIndex("birthdate"));
                String strPhone = cursor.getString(cursor.getColumnIndex("phone"));
                String strRemark = cursor.getString(cursor.getColumnIndex("remark"));
//                String info = strName + strBirthDate + strPhone + strRemark;
//                Log.i("info",info);

                BirthInfo birthInfo = new BirthInfo();
                birthInfo.strName = strName;
                birthInfo.strBirthdate = strBirthDate;
                birthInfo.strPhone = strPhone;
                birthInfo.strRemark = strRemark;

                mListBirthInfo.add(birthInfo);
            }
            cursor.close();
            birthAdapter.setmListBirthInfo(mListBirthInfo);
            birthAdapter.notifyDataSetChanged();
        }
        //db.close();  //数据库不能关闭

    }



}
